package com.example.t470p.facechecktickets.Utils;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import android.os.Environment;

public class util_File{
	public static  String ExternalPath=null;
	
	
	public util_File(){
		
	}
	public  static boolean inti_util_File(){	
		if (Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED))
		{
			ExternalPath=getExternalPath();
		}
		else
		{
			return false;
		}
		
		return ExternalPath == null ? false:true;
	}
	public static String getExternalPath(){
    	 try {
    		 ExternalPath=Environment.getExternalStorageDirectory().getCanonicalPath()+"/";
			return ExternalPath;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    	 return null;
     }


	public	static boolean createFolderIfNotExists(String strFolder) {
		try{
			File file = new File(strFolder);
			if (!file.exists())
			{
				if (file.mkdirs())
					return true;
				else
					return false;
			}

		}catch (Exception e) {
			// TODO: handle exception
			return false;
		}
		return true;
	}
	public static boolean fileIsExists(String filename){
        try{
                File f=new File(ExternalPath+filename);
                if(!f.exists()){
                        return false;
                }
                
        }catch (Exception e) {
                // TODO: handle exception
                return false;
        }
        return true;
	}
	/**
	 * 将字符串写入SD卡中的文件
	 * @param fileName
	 * 文件名，写入SD卡式需先用getExternalPath()获得外部路径
	 * @param write_str
	 * 写入的字符串
	 * @return
	 */
	public static boolean writeStr2SdcardFile(String fileName,String write_str){ 
			boolean ret=false;
		try{ 
		       FileOutputStream fout = new FileOutputStream(fileName); 
		       byte [] bytes = write_str.getBytes(); 			
		       fout.write(bytes); 
		       fout.close(); 
		       ret=true;
			}
			catch(Exception e){ 
			     e.printStackTrace(); 			      
			} 
			return ret;
	
	 } 
	public static boolean writeByte2SdcardFile(String fileName,byte write_byte[]){ 
		boolean ret=false;
		 try{ 
		       FileOutputStream fout = new FileOutputStream(fileName); 		
		       fout.write(write_byte); 
		       fout.flush();
		       fout.close(); 
		       ret=true;
		    }		
		  catch(Exception e){ 
		       e.printStackTrace(); 
		  } 
		 return ret;
	} 
		
	public static boolean appendByte2SdcardFile(String fileName,byte write_byte[]){ 
		boolean ret=false;
		 try{ 
		       FileOutputStream fout = new FileOutputStream(fileName,true); 
		      // byte [] bytes = write_str.getBytes(); 
		       fout.write(write_byte);
		       fout.flush();
		       fout.close(); 
		       ret=true;
		     }		
		     catch(Exception e){ 
		        e.printStackTrace(); 
		     } 
		 return ret;
		
	} 
		
	  
	//读SD中的文件
	public static byte [] readAllSdcardFile(String fileName){ 
	 // String res=""; 
	  byte [] buffer = null;

		try{ 
	         FileInputStream fin = new FileInputStream(fileName); 	
	         int length = fin.available(); 	
	         buffer = new byte[length]; 
	         fin.read(buffer);     
	         fin.close();     
	     } 
	       catch(Exception e){ 
	        	e.printStackTrace(); 
	        	return null;
	      } 
	      return buffer; 
	} 
	
	public static boolean copyfile(String src, String des){
		 boolean ret=false;
		 try{
			 FileInputStream fin = new FileInputStream(src); 
			 FileOutputStream fout = new FileOutputStream(des);
			 int length=0;
			 byte[] buffer = new byte[2048];
			 while((length=fin.read(buffer))>0)
				 fout.write(buffer,0,length);
			 fin.close();  
			 fout.flush();
		     fout.close(); 
		     ret=true;
		 }
		 catch(Exception e){
			 e.printStackTrace();
		 }
		 return ret;
	 }
	 
	 public	static boolean isFolderExists(String strFolder) {
			try{
				File file = new File(strFolder);
				if (!file.exists()) 
				{  		          
					if (file.mkdirs())                                 
						return true;            
					else                
						return false;                  
				}  
	        
			}catch (Exception e) {
	        // TODO: handle exception
				return false;
			}       
			return true;   
		}
	 
	 public static boolean rename(String oldpath,String newpath){
		 File file=new File(oldpath);
		 return file.renameTo(new File(newpath));
	 }
	 
	 
	 //读取asset文件夹资源例子
/*		private boolean copyDb()
		{	

			String Dbfilename="/synjones/idcard/db/idcard.db";
		 	InputStream inputStream = null; 
		 	AssetManager assetManager = getAssets(); 
		 	byte inDb[]=null;
		 	try { 
		
		 		inputStream = assetManager.open("idcard.db"); 
		 		inDb=new byte[inputStream.available()];
		 		inputStream.read(inDb);
		 		write2SdcardFile(Dbfilename,inDb);
		 	} catch (IOException e) { 
		 		log.debug("copyDb() err "+ e.getMessage()); 
		 		return false;
		 	} 
		 	return true;		  
		}
	*/
	
}